package XML;


import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import Client.ClientDetails;

public class ConfigHelper 
{

	/**
	 * Create dynamic a hibernate.hhbm.xml
	 * @param userNameDB
	 * @param passwordDB
	 * @param mainServerFolder
	 * @throws Exception
	 */
	public static ClientDetails readXmlClient() throws Exception
	{
		 try {
			 String strFolderPath = null;
			 String strUserName = null;
			 String strPassword = null;
			 String strEmail = null;
	            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
	            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
	            Document doc = docBuilder.parse (new File("C:\\ourDropBox\\User\\ClientDetails.xml"));

	            // normalize text representation
	            doc.getDocumentElement ().normalize ();
	            System.out.println ("Root element of the doc is " + 
	                 doc.getDocumentElement().getNodeName());


	            NodeList listOfClient= doc.getElementsByTagName("client");
	            int totalClients = listOfClient.getLength();
	            System.out.println("Total no of people : " + totalClients);

	            for(int s=0; s<listOfClient.getLength() ; s++){


	                Node firstClientNode = listOfClient.item(s);
	                if(firstClientNode.getNodeType() == Node.ELEMENT_NODE){


	                    Element firstClientElement = (Element)firstClientNode;

	                    //-------user name
	                    NodeList userNameList = firstClientElement.getElementsByTagName("userName");
	                    Element userNameElement = (Element)userNameList.item(0);
	                    NodeList textUNList = userNameElement.getChildNodes();
	                    strUserName=((Node)textUNList.item(0)).getNodeValue().trim().toString();
	                    System.out.println("User Name : " + 
	                           ((Node)textUNList.item(0)).getNodeValue().trim());

	                    //-------password
	                    NodeList passwordList = firstClientElement.getElementsByTagName("password");
	                    Element passwordElement = (Element)passwordList.item(0);
	                    NodeList textPList = passwordElement.getChildNodes();
	                    strPassword=((Node)textPList.item(0)).getNodeValue().trim().toString();
	                    System.out.println("Password : " + 
	                           ((Node)textPList.item(0)).getNodeValue().trim());

	                    //----email
	                    NodeList emailList = firstClientElement.getElementsByTagName("email");
	                    Element emailElement = (Element)emailList.item(0);

	                    NodeList textEmailList = emailElement.getChildNodes();
	                    strEmail=((Node)textEmailList.item(0)).getNodeValue().trim().toString();
	                    System.out.println("email : " + 
	                           ((Node)textEmailList.item(0)).getNodeValue().trim());

	                    //----folder path list
	                    NodeList folderPathList = firstClientElement.getElementsByTagName("folderPath");
	                    Element folderPathElement = (Element)folderPathList.item(0);

	                    NodeList textFolderPathList = folderPathElement.getChildNodes();
	                    strFolderPath=((Node)textFolderPathList.item(0)).getNodeValue().trim().toString();
	                    System.out.println("folder path : " + 
	                           ((Node)textFolderPathList.item(0)).getNodeValue().trim());

	                    //------

	                }//end of if clause


	            }//end of for loop with s var
	            return new ClientDetails(strUserName, strPassword, strFolderPath, strEmail);

	        }catch (SAXParseException err) {
	        System.out.println ("** Parsing error" + ", line " 
	             + err.getLineNumber () + ", uri " + err.getSystemId ());
	        System.out.println(" " + err.getMessage ());

	        }catch (SAXException e) {
	        Exception x = e.getException ();
	        ((x == null) ? e : x).printStackTrace ();

	        }catch (Throwable t) {
	        t.printStackTrace ();
	        }
		return null;
	}

}